package com.moma.chatting.core;

import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConversationObserver implements Runnable {
	private long interval;
	private HashMap<Integer, Conversation> conversationMap;
	private final static Logger logger;
	
	static{
		logger = LoggerFactory.getLogger(ConversationObserver.class);
	}
	public ConversationObserver(HashMap<Integer, Conversation> conversationMap, long interval){
		this.interval = interval;
		this.conversationMap = conversationMap;
	}
	
	public void run() {
		logger.info("Observer is started...");
		while (true){
			logger.info("Observer is triggered...");
			for (Conversation conv : this.conversationMap.values()){
				DynamicGroup group = conv.getGroup();
				boolean isAlive = group.getIsAlive();
				if (!isAlive && !conv.getRecyclerFlag()){
					conv.setRecyclerFlag(true);
					logger.info(String.format("Conversation %d is set to [need recycle]...", conv.getConversationId()));
				}
			}
			try {
				Thread.sleep(interval);
			} catch (Exception e) {
				logger.error("Observer throws exception..." + e.getStackTrace());
			}
		}
	}
}
